#!/bin/bash -e

# Date 		: 2020.12.20
# Author	: majin
# Mail 		: xx
# Function	: add some preset in extra bashrc for work environment
# Version 	: 1.0
# Update 	: 2021.01.18

# Log printing, set the printing level and distinguish by color, easy to debug

RES="\033[0m"
GREEN="\033[32m"
BLUE="\033[36m"
YELLOW="\033[33m"
PURPLE="\033[35m"
RED="\033[31m"

name_msg(){ echo -e "${GREEN}name_msg:  [ `readlink -f $0` ] ${RES}";}
info_msg(){ echo -e "${BLUE}info_msg: $1 ${RES}";}
debug_msg(){ echo -e "${YELLOW}debug_msg: $1 ${RES}";}
warn_msg(){ echo -e "${PURPLE}warn_msg: $1 ${RES}";}
error_msg(){ echo -e "${RED}error_msg: $1 ${RES}";}

echo_test(){ name_msg;info_msg;debug_msg;warn_msg;error_msg;}

# Check whether there are additional bashrc scripts in the $HOME directory. 
# If not, copy the current script to the $HOME directory
if [[ ! -f "$HOME/.extra_bashrc" ]];then
    warn_msg "$HOME/.extra_bashrc is not exist, begin to create!" 
	info_msg "running: \`cp $(readlink -f $0) $HOME/.extra_bashrc -f\` "	
	cp $(readlink -f $0) $HOME/.extra_bashrc -f;
fi

# Check whether `source ~/.extra_bashrc` has been set in bashrc script. 
# If not, write and add source ~ /.extra_bsahrc
flag=`grep "source ~/.extra_bashrc" $HOME/.bashrc`
if [[ ! $flag ]];then
    warn_msg "~/.bashrc is not updated for extra_bashrc, begin to set!" 
    echo "" >> $HOME/.bashrc;
	echo "# majin: extra_bashrc" >> $HOME/.bashrc;
	echo "source ~/.extra_bashrc" >> $HOME/.bashrc; 
    info_msg "~/.bashrc is updated for extra_bashrc, make sure for \`source ~/.bashrc\`"
fi

# Set up personalized terminal.
# The first one is suitable for some terminal software, such as mabaxterm. 
# The second is a simplified version, suitable for web pages such as jump.
# PS1="\[\e[31;40m\]`echo $'\ue0f6'`\[\e[0m\]\[\e[30;46m\]`echo $'\ue0e9'` \t \[\e[0m\]\[\e[36;42m\]`echo $'\ue0b0'`\[\e[0m\]\[\e[30;42m\] `echo $'\ue0ed'` \u \[\e[0m\]\[\e[32;43m\]`echo $'\ue0b0'`\[\e[0m\]\[\e[30;43m\] `echo $'\ue0ea'` \W \[\e[0m\]\[\e[33;40m\]`echo $'\ue0b0'`\[\e[0m\] "
# PS1="\[\e[31;40m\][\u]\[\e[0m\]\[\e[35;40m\]{\W}\[\e[0m\]\[\e[33;40m\]->\[\e[0m\] "

# Some help on Git is mainly for the convenience of transplantation
majin_help(){
	nowdir=`pwd`
    tmp_dir="$HOME/tmp"
	if [[ ! -d "$tmp_dir" ]];then
		cd $HOME
		git clone https://gitee.com/ijinma/tmp.git
		
	fi
	cd $tmp_dir;
	case $1 in
	pull)
		git pull;
		cat $tmp_dir/README.md;	
		cd $nowdir
		;;
	add)
		if [[ -n "$2" ]];then
			vim $2
		fi
		git add .
		git commit -m "update note"
		git push
		cd $nowdir
		;;
	*)
		cat $tmp_dir/README.md;	
		cd $nowdir
		;;
	esac
    echo ""
}

# Some aliases of bash operation are easy to set quickly
alias sourcebash="source ~/.bashrc && info_msg \"areadly source ~/.bashrc\""
alias vibash="vi ~/.bashrc && sourcebash"
alias viextrabash="vi ~/.extra_bashrc && sourcebash"
alias ..="cd .."
alias cd..="cd .."
# In order to switch the directory quickly, some paths are pre alias set
if [[ ! -d $HOME/install ]];then
    info_msg "create dir $HOME/install"
    mkdir -p $HOME/install 
fi
alias cdinstall="cd $HOME/install; info_msg `pwd`"

install_dir="deps tools gcc os"
for i in ${install_dir}
do
    if [[ ! -d $HOME/install/${i}_install ]];then
        info_msg "create dir ${i}_install/pkgs"
        mkdir -p $HOME/install/${i}_install/pkgs
    fi
    alias cd${i}_install="cd $HOME/install/${i}_install; info_msg `pwd`"
done

if [[ ! -d $HOME/test ]];then
    info_msg "create dir $HOME/test"
    mkdir -p $HOME/test 
fi
alias cdtest="cd $HOME/test; info_msg `pwd`"

# add some evn PATH
# export PATH=$PATH
# export LIBRARY_PATH=$LIBRARY_PATH
# export CC=gcc
# export CXX=g++
# export LD_LIBRARY_PATH=$LD_LIBRARY_PATH
